package com.example.star.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.star.R;
import com.example.star.contract.UserContract;
import com.example.star.entity.User;
import com.example.star.presenter.UserPresenter;


public class RegisterActivity extends BaseActivity implements UserContract.View {
    private EditText mRegisterUsername;
    private EditText mRegisterNickname;
    private EditText mRegisterPassword;
    private EditText mRegisterRepassword;
    private Button mBtnRegister;
    private UserContract.Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        mRegisterUsername = findViewById(R.id.register_username);
        mRegisterNickname = findViewById(R.id.register_nickname);
        mRegisterPassword = findViewById(R.id.register_password);
        mRegisterRepassword = findViewById(R.id.register_repassword);
        mBtnRegister = findViewById(R.id.btn_Register);

        mBtnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = mRegisterUsername.getText().toString().trim();
                String nickname = mRegisterNickname.getText().toString().trim();
                String password = mRegisterPassword.getText().toString().trim();
                String repassword = mRegisterRepassword.getText().toString().trim();

                if (username.isEmpty() || password.isEmpty() || repassword.isEmpty() || nickname.isEmpty()){
                    Toast.makeText(RegisterActivity.this,getString(R.string.username_password_repassword),Toast.LENGTH_SHORT).show();
                } else if (!password.equals(repassword)){
                    showPasswordMismatch();
                }else {
                    User user = new User(username,nickname,password);
                    user.setUsername(username);
                    user.setNickname(nickname);
                    user.setPassword(password);
                    presenter.register(user);
                }
            }
        });
        presenter = new UserPresenter(this);
        setTitle("注册");
        setUpToolBar();
    }
    @Override
    public void showPasswordMismatch(){
        Toast.makeText(this,getString(R.string.password_mismatch),Toast.LENGTH_SHORT).show();
    }
    @Override
    public void showLoading() {
        // 显示加载框
    }

    @Override
    public void hideLoading() {
        // 隐藏加载框
    }
    @Override
    public void onLoginSuccess() {
        // 这里不需要处理
    }

    @Override
    public void onLoginFailure(String message) {
        // 这里不需要处理
    }
    @Override
    public void onRegisterSuccess() {
        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this,LoginActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onRegisterFailure(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
    @Override
    public void showError(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

}